<?php
class FeedAction extends Action {
/**
	 * 添加微博
	 * @param integer $uid 操作用户ID
	 * @param string $app 微博应用类型，默认为public
	 * @param string $type 微博类型，
	 * @param array $data 微博相关数据
	 * @param integer $app_id 应用资源ID，默认为0
	 * @param string $app_table 应用资源表名，默认为feed
	 * @param array  $extUid 额外用户ID，默认为null
	 * @param array $lessUids 去除的用户ID，默认为null
	 * @param boolean $isAtMe 是否为进行发送，默认为true
	 * @return mix 添加失败返回false，成功返回新的微博ID
	 */
	public function postFeed(){//【发表动态】
	$type = 'post';
	$feed = model('Feed');
	$data['content'] = $_POST['content'];
	$data['body'] = $data['content'];
	$uid = $_POST['uid'];
	$app = 'public';
	if($_FILES){//判断是否有上传图片，有就上传
		$upload_info = $this->uploadImage();
		if(!$upload_info){
			$value = ['code'=>'wrong'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$attach = model('Attach');
		$option = array(
			
		);
		$upload_info = $attach->saveInfo($upload_info);
		
		foreach ($upload_info as $info){
			$info['save_path'] = 'User/'.date("Ymd").'/';
			$result = $attach->data($info)->add();
		
			$data['attach_id'][] = $result;
		}
		$type = 'postimage';
	}
	//动态写入数据库
	$result = $feed->put($uid,$app,$type,$data);
	if($result){
		$data = $feed->getFeedInfo($result['feed_id']);
		unset($data['source_body']);
		//dump($data);exit;
		$value = ['code'=>'success','data'=>$data];
		header("Content-Type: text/json");
		die(json_encode($value));
		
	}else{
		$value = ['code'=>'flase'];
		header("Content-Type: text/json");
		die(json_encode($value));
	}
	}
	
	public function deleteFeed(){
		$feed_id = $_REQUEST['feed_id'];
		$uid = $_REQUEST['uid'];
		
		$feed = M('Feed');
		$find = $feed->where("feed_id = $feed_id and uid = $uid")->find();
		if(!$find){//找不到
			$value = ['code'=>'notfound'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$delete = model('Feed')->doEditFeed($feed_id,'deleteFeed');
		if($delete){
			M('Feed_Like')->where("feed_id = $feed_id")->delete();
			$value = ['code'=>'success'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function uploadImage(){//【上传动态附带图片】
		
		$upload	= new UploadFile();
		
				$upload= new UploadFile();
				$upload->autoSub=true; //开启子目录保存文件
				$upload->maxSize = 2097152;//最大为2m
				$upload->exts=array('jpg','jpeg','gif','png','bmp');//可上传的类型
				$upload->savePath='data/upload/User/'.date("Ymd").'/';
				
				$info = $upload->upload();
				
					if($info){
							$result= $upload->getUploadFileInfo();
							
						}else{
							
							return  false;
						}
				
				
				return $result;
	
	}
	
	public function getFollowingList(){//【获取动态列表】
		$var['new']=0;	//是否要显示最新
		//$var['type']='following';	//微博类型
		$var['cancomment']=1;	//是否可评论
		$var['feed_key']='';	//搜索关键词
		//$var['loadId'] = $_POST['loadId'];		//最后显示的feed_id
		//$uid = '2';
		$limit = 10;
		$var['type']= $_POST['type'];
		$var['loadId'] = $_POST['loadid'];
		$uid = $_POST['uid'];
		$var['feed_key'] = t($var['feed_key']);
        $var['cancomment'] = isset($var['cancomment']) ? $var['cancomment'] : 1;
        //$var['cancomment_old_type'] = array('post','repost','postimage','postfile');
        $var['cancomment_old_type'] = array('post','repost','postimage','postfile','weiba_post','weiba_repost');
        // 获取微博配置
        $weiboSet = model('Xdata')->get('admin_Config:feed');
        $var = array_merge($var, $weiboSet);
    	$var['remarkHash'] = model('Follow')->getRemarkHash($uid);
    	$map = $list = array();
    	$type = $var['new'] ? 'new'.$var['type'] : $var['type'];	// 最新的微博与默认微博类型一一对应

    	switch($type) {
    		case 'following':// 我关注的
    			if(!empty($var['feed_key'])){
    				//关键字匹配 采用搜索引擎兼容函数搜索 后期可能会扩展为搜索引擎
    				$list = model('Feed')->searchFeed($var['feed_key'],'following',$var['loadId'],$limit);
    			}else{
    				$where =' (a.is_audit=1 OR a.is_audit=0 AND a.uid='.$uid.') AND a.is_del = 0 ';
    				if($var['loadId'] > 0){ //非第一次
    					$where .=" AND a.feed_id < '".intval($var['loadId'])."'";
    				}
    				if(!empty($var['feed_type'])){
    					$where .=" AND a.type = '".t($var['feed_type'])."'";
    				}

    				$list =  model('Feed')->getFollowingFeed($where,$limit,$uid);
    				$list = $list['data'];
    				if(!$list){
    					$value = ['code'=>'false'];
    					header("Content-Type: text/json");
						die(json_encode($value));
    				}
    				$i=0;
    				//dump($list);exit;
    				foreach ($list as $d){
    					$result[$i]['user_info']=$d['user_info'];
						$result[$i]['feed_data'] = model('Feed')->getFeedInfo(($d['feed_id']));
						$uid = $d['uid'];
						$avatar = M('User')->where("uid = $uid")->field('avatar')->find();
						$result[$i]['feed_data']['avatar'] = $avatar['avatar'];
						//$result[$i]['feed_data']['comment'] = $this->getAllcomment($d['feed_id']);
						$allComment = $this->getAllcomment($d['feed_id']);
						if($allComment!=null){
						foreach ($allComment as $key=>$c){
							if($c['to_uid']){
								$to_uid = $c['to_uid'];
								$user = M('User')->where("uid = $to_uid")->field("uname")->find();
								$allComment[$key]['to_uname'] = $user['uname'];
							}
						
						}
						}
						$result[$i]['feed_data']['comment'] = $allComment;
						$result[$i]['feed_data']['like'] = $this->getLike($d['feed_id']);
						unset($result[$i]['feed_data']['feed_content']);
						unset($result[$i]['feed_data']['avatar_big']);
						unset($result[$i]['feed_data']['avatar_middle']);
						unset($result[$i]['feed_data']['avatar_small']);
						unset($result[$i]['feed_data']['client_ip']);
						unset($result[$i]['feed_data']['comment_all_count']);
						unset($result[$i]['feed_data']['is_del']);
						unset($result[$i]['feed_data']['app_row_table']);
						unset($result[$i]['feed_data']['transpond_id']);
						unset($result[$i]['feed_data']['app']);
						unset($result[$i]['feed_data']['source_body']);
						unset($result[$i]['feed_data']['transpond_data']);
						unset($result[$i]['user_info']);
						$i++;
					}
					$load = end($result);
					//$result['loadId'] = $load['feed_data']['feed_id'];
					$load_id = $load['feed_data']['feed_id'];
    				//dump(array_replace("sns","",$result));exit;
    				//dump($result);exit;
    				if(!empty($result)){
    					
    					$value = ['code'=>'success','loadid'=>$load_id,'data'=>$result];
    					//dump($value);exit;
    					header("Content-Type: text/json");
    					die(json_encode($value));
    				}else{
    					$value = ['code'=>'false'];
    					header("Content-Type: text/json");
						die(json_encode($value));
    				}
    			}
    			break;
    		case 'all'://所有的 动态
    			if(!empty($var['feed_key'])){
    				//关键字匹配 采用搜索引擎兼容函数搜索 后期可能会扩展为搜索引擎
    				$list = model('Feed')->searchFeed($var['feed_key'],'all',$var['loadId'],$limit);
    			}else{
                    $where =' (is_audit=1 OR is_audit=0 AND uid='.$uid.') AND is_del = 0 ';
    				if($var['loadId'] > 0){ //非第一次
    					$where .=" AND feed_id < '".intval($var['loadId'])."'";
    				}
    				if(!empty($var['feed_type'])){
    					$where .=" AND type = '".t($var['feed_type'])."'";
    				}
    				$list = model('Feed')->getList($where,$limit);
    				$list = $list['data'];
    				
    				$i=0;
    				
    			foreach ($list as $d){
    				$result[$i]['user_info']=$d['user_info'];
						$result[$i]['feed_data'] = model('Feed')->getFeedInfo(($d['feed_id']));
						$uid = $d['uid'];
						$avatar = M('User')->where("uid = $uid")->field('avatar')->find();
						$result[$i]['feed_data']['avatar'] = $avatar['avatar'];
						//$result[$i]['feed_data']['comment'] = $this->getAllcomment($d['feed_id']);
						$allComment = $this->getAllcomment($d['feed_id']);
						if($allComment!=null){
						foreach ($allComment as $key=>$c){
							if($c['to_uid']){
								$to_uid = $c['to_uid'];
								$user = M('User')->where("uid = $to_uid")->field("uname")->find();
								$allComment[$key]['to_uname'] = $user['uname'];
							}
						
						}
						}
						$result[$i]['feed_data']['comment'] = $allComment;
						$result[$i]['feed_data']['like'] = $this->getLike($d['feed_id']);
						unset($result[$i]['feed_data']['feed_content']);
						unset($result[$i]['feed_data']['avatar_big']);
						unset($result[$i]['feed_data']['avatar_middle']);
						unset($result[$i]['feed_data']['avatar_small']);
						unset($result[$i]['feed_data']['client_ip']);
						unset($result[$i]['feed_data']['comment_all_count']);
						unset($result[$i]['feed_data']['is_del']);
						unset($result[$i]['feed_data']['app_row_table']);
						unset($result[$i]['feed_data']['transpond_id']);
						unset($result[$i]['feed_data']['app']);
						unset($result[$i]['feed_data']['source_body']);
						unset($result[$i]['feed_data']['transpond_data']);
						unset($result[$i]['user_info']);
						$i++;
				}
					$load = end($result);
					//$result['loadId'] = $load['feed_data']['feed_id'];
					$load_id = $load['feed_data']['feed_id'];
    				if(!empty($result)){
    					$value = ['code'=>'success','loadid'=>$load_id,'data'=>$result];
    					//dump($value);exit;
    					header("Content-Type: text/json");
    					die(json_encode($value));
    				}else{
    					$value = ['code'=>'false'];
    					header("Content-Type: text/json");
						die(json_encode($value));
    				}
    				
    			}
    			break;
    		
    	}
	}
	
	public function getRecent(){
		$uid = $_REQUEST['uid'];
		$map->uid = $uid;
		$limit = 5;
		$feed = model('Feed')->getList($map,$limit);
		if($feed){
			$list = $feed['data'];
    			$i=0;
    			foreach ($list as $d){
    				$result[$i]['user_info']=$d['user_info'];
						$result[$i]['feed_data'] = model('Feed')->getFeedInfo(($d['feed_id']));
						$uid = $d['uid'];
						$avatar = M('User')->where("uid = $uid")->field('avatar')->find();
						$result[$i]['feed_data']['avatar'] = $avatar['avatar'];
						//$result[$i]['feed_data']['comment'] = $this->getAllcomment($d['feed_id']);
						$allComment = $this->getAllcomment($d['feed_id']);
						if($allComment!=null){
						foreach ($allComment as $key=>$c){
							if($c['to_uid']){
								$to_uid = $c['to_uid'];
								$user = M('User')->where("uid = $to_uid")->field("uname")->find();
								$allComment[$key]['to_uname'] = $user['uname'];
							}
						
						}
						}
						$result[$i]['feed_data']['comment'] = $allComment;
						$result[$i]['feed_data']['like'] = $this->getLike($d['feed_id']);
						unset($result[$i]['feed_data']['feed_content']);
						unset($result[$i]['feed_data']['avatar_big']);
						unset($result[$i]['feed_data']['avatar_middle']);
						unset($result[$i]['feed_data']['avatar_small']);
						unset($result[$i]['feed_data']['client_ip']);
						unset($result[$i]['feed_data']['comment_all_count']);
						unset($result[$i]['feed_data']['is_del']);
						unset($result[$i]['feed_data']['app_row_table']);
						unset($result[$i]['feed_data']['transpond_id']);
						unset($result[$i]['feed_data']['app']);
						unset($result[$i]['feed_data']['source_body']);
						unset($result[$i]['feed_data']['transpond_data']);
						unset($result[$i]['user_info']);
						$i++;
				}
				
				if(!empty($result)){
    					$value = ['code'=>'success','data'=>$result];
    					//dump($value);exit;
    					header("Content-Type: text/json");
    					die(json_encode($value));
    				}else{
    					$find = M('User')->where("uid = $uid")->find();
    					if($find){
    						$value = ['code'=>'success','data'=>array()];
    						header("Content-Type: text/json");
							die(json_encode($value));
    					}else{
    						$value = ['code'=>'false'];
    						header("Content-Type: text/json");
							die(json_encode($value));
    					}
    				}
		}else{
			
				$value = ['code'=>'false'];
    			header("Content-Type: text/json");
				die(json_encode($value));
		}
	
	}
	
	public function getUserFeed(){//【个人动态列表】
		$uid = $_REQUEST['uid'];
		$map->uid = $uid;
		$loadid = $_REQUEST['loadid'];
		if($loadid>0){
			$map->feed_id = array('gt',$loadid);
		}
		$limit = 10;
		$feed = model('Feed')->getList($map,$limit);
		if($feed){
			$list = $feed['data'];
    			$i=0;
    			foreach ($list as $d){
    				$result[$i]['user_info']=$d['user_info'];
						$result[$i]['feed_data'] = model('Feed')->getFeedInfo(($d['feed_id']));
						$uid = $d['uid'];
						$avatar = M('User')->where("uid = $uid")->field('avatar')->find();
						$result[$i]['feed_data']['avatar'] = $avatar['avatar'];
						//$result[$i]['feed_data']['comment'] = $this->getAllcomment($d['feed_id']);
						$allComment = $this->getAllcomment($d['feed_id']);
						if($allComment!=null){
						foreach ($allComment as $key=>$c){
							if($c['to_uid']){
								$to_uid = $c['to_uid'];
								$user = M('User')->where("uid = $to_uid")->field("uname")->find();
								$allComment[$key]['to_uname'] = $user['uname'];
							}
						
						}
						}else{
							$allComment = array();
						}
						$result[$i]['feed_data']['comment'] = $allComment;
						$result[$i]['feed_data']['like'] = $this->getLike($d['feed_id']);
						unset($result[$i]['feed_data']['feed_content']);
						unset($result[$i]['feed_data']['avatar_big']);
						unset($result[$i]['feed_data']['avatar_middle']);
						unset($result[$i]['feed_data']['avatar_small']);
						unset($result[$i]['feed_data']['client_ip']);
						unset($result[$i]['feed_data']['comment_all_count']);
						unset($result[$i]['feed_data']['is_del']);
						unset($result[$i]['feed_data']['app_row_table']);
						unset($result[$i]['feed_data']['transpond_id']);
						unset($result[$i]['feed_data']['app']);
						unset($result[$i]['feed_data']['source_body']);
						unset($result[$i]['feed_data']['transpond_data']);
						unset($result[$i]['user_info']);
						$i++;
				}
					$load = end($result);
					//$result['loadId'] = $load['feed_data']['feed_id'];
					$load_id = $load['feed_data']['feed_id'];
				if(!empty($result)){
    					$value = ['code'=>'success','load_id'=>$load_id,'data'=>$result];
    					//dump($value);exit;
    					header("Content-Type: text/json");
    					die(json_encode($value));
    				}else{
    					$find = M('User')->where("uid = $uid")->find();
    					if($find){
    						$value = ['code'=>'success','data'=>array()];
    						header("Content-Type: text/json");
							die(json_encode($value));
    					}else{
    						$value = ['code'=>'false'];
    						header("Content-Type: text/json");
							die(json_encode($value));
    					}
    				}
		}else{
			
				$value = ['code'=>'false'];
    			header("Content-Type: text/json");
				die(json_encode($value));
		}
	
	}
	
	
	public function addLike(){
		$uid = $_REQUEST['uid'];
		$feed_id = $_REQUEST['feed_id'];
		$feed = model("Feed")->getFeedInfo($feed_id);
		$feed_like = M("Feed_like");
		$find = $feed_like->where("feed_id = $feed_id and uid = $uid")->find();
		if($find){
			$value = ['code'=>'liked'];
    		header("Content-Type: text/json");
			die(json_encode($value));
		}
		$feed_like->feed_id = intval($feed_id);
		$feed_like->uid = intval($uid);
		$feed_like->app_uid = $feed['uid'];
		$feed_like->ctime = time();
		//dump($feed_like);exit;
		if($feed_like->add()){
			
			$notify = new NotifyAction();
			$notify->addNotify($feed['uid'], 'like');
			$data = $this->getLike($feed_id);
			$value = ['code'=>'success','data'=>$data];
    		header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ['code'=>'false'];
    		header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	
	public function unLike(){
		$uid = $_REQUEST['uid'];
		$feed_id = $_REQUEST['feed_id'];
		$feed_like = M("Feed_like");
		$find = $feed_like->where("feed_id = $feed_id and uid = $uid")->find();
		if(!$find){
			$value = ['code'=>'notfound'];
    		header("Content-Type: text/json");
			die(json_encode($value));
		}
		$lid = $find['lid'];
		$delete = $feed_like->where("lid = $lid")->delete();
		if($delete){
			$data = $this->getLike($feed_id);
			$value = ['code'=>'success','data'=>$data];
    		header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ['code'=>'false'];
    		header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function getLike($feed_id){
		$feed_like = M("Feed_like");
		$data = $feed_like->where("feed_id = $feed_id")->order("ctime desc")->select();
		if($data){
			return $data;
		}else{
			return array();
		}
	}
	
	public function getAllcomment($row_id){//获取所有评论
	
		$comment = M('Comment');
		$data = $comment->where("row_id = $row_id")->order("ctime asc")->field('comment_id,uid as cid,content,to_uid,ctime')->select();
		//dump($data);
		if($data){
			$user = M('User');
			foreach ($data as $key=>$d){
			$uid = $d['cid'];
			$info = $user->where("uid = $uid")->field("uname")->find();
			$data[$key]['uname'] = $info['uname'];
			}
			return $data;
		}else{
			return null;
		}
	}
	
	public function shareFeed(){
		$feed_id = $_REQUEST['feed_id'];
		$data = model("Feed")->getFeedInfo($feed_id);
		$this->assign("avatar",$data['avatar']);
		//$this->assign("avatar","http://yjkang.vba8.com/data/upload/user/avatar/8156397630ab868.jpg");
		$down_url = model("Xdata")->getConfig("url","appDown","admin_Config");
		$down_img = model("Xdata")->getConfig("img","appDown","admin_Config");
		$img = M("Attach")->where("attach_id = $down_img")->find();
		$down_img = getImageUrl($img['save_path'].$img['save_name']);
		$this->assign("down_url",$down_url);
		$this->assign("down_img",$down_img);
		$this->assign("uname",$data['uname']);
		$this->assign("content",$data['content']);
		$this->assign("attach",$data['attach']);
		$this->display();
	
	}
	
	
}